#!/bin/bash
[[ $DEBUG ]] && set -uxe

sleep 10
PORT=51002
#HOSTNAME=$1
#SIZE=$2
#PASSWORD=$3
slot_num=$((16384 / ${SIZE}))
for (( i = 0; i < ${SIZE}; i++ )); do
        ./redis-cli -h ${HOSTNAME} -p ${PORT} -a "${PASSWORD}" cluster meet ${SERVICE_NAME}-(($i+1))  ${PORT}
        ./redis-cli -h ${HOSTNAME} -p ${PORT} -a "${PASSWORD}" cluster meet ${SERVICE_NAME}-(($i+2))  ${PORT}
done


echo "cluster meet done......"
sleep 10

./redis-cli -h ${HOSTNAME}-1 -p ${PORT} -a ${PASSWORD}  cluster nodes
for (( i = 0; i < ${SIZE}; i++ )); do
        uuid=$(./redis-cli -h ${HOSTNAME}-1 -p ${PORT} -a ${PASSWORD}  cluster nodes | grep ${HOSTNAME}.$((10 + $i)) | awk '{print $1}')
        ./redis-cli -h ${HOSTNAME}.$((20 + $i)) -p ${PORT}  -a ${PASSWORD} cluster replicate ${uuid}
done
for (( i = 0; i < $((${SIZE} - 1 )); i++ )); do
        ./redis-cli -h ${HOSTNAME}.$((10 + $i)) -p ${PORT} -a ${PASSWORD} cluster addslots {$((${i}*${slot_num}))..$((${slot_num}*(${i}+1)-1))}
done
./redis-cli -h ${HOSTNAME}.$((10 + ${SIZE} - 1)) -p ${PORT} -a ${PASSWORD} cluster addslots {$(((${SIZE}-1)*${slot_num}))..16383}


echo "cluster build done.."

[[ $PAUSE ]] && sleep $PAUSE

sleep 5
./redis-cli -h ${HOSTNAME}.10 -p ${PORT}  -a ${PASSWORD} cluster nodes